/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.SettingBean;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

/**
 * @author Feng haitao
 * 
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/setting")
public class SettingTotalAction extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5236170890634487270L;
	private Logger logger = LogManager.getLogger(SettingTotalAction.class);
	private Operators operators;
	private List<Object> profitRsList = new ArrayList<Object>();
	private SettingTotalSearchCondition searchCondition;
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	
	
	
	

	private String msg;

	@Autowired
	IPagenitionService pagenitionService;

	@Autowired
	IorgService orgService;
	@Autowired
	ISettflagService settflagService;
	@Autowired
	IMerchantServices merchantServices;
	
	// 结算查询
	@Action(value = "settingtotalsearch", results = {
			@Result(name = "success", location = "/pages/setting/settingtotallist.jsp"),
			@Result(name = "error", location = "/pages/setting/settinglist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp") })
	public String settingtotalsearch() {
		Operators logingOperators = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		
		

		Map<String, Operators> merchantMap = new TreeMap<String, Operators>();
		if (searchCondition == null) {
			searchCondition = (SettingTotalSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new SettingTotalSearchCondition();
			}
		}
		try {
			orgList=orgService.listOrgs(logingOperators);
			// 系统管理员查看全部，代理只能看到本机构下的。
			//searchCondition.setRoleType(logingMerchant.getRoleType());
			if(Constance.OPERATORS_FLAG_OEM.equals(logingOperators.getFlag())){
				if(Role.MERCHANT.getCode().equals(logingOperators.getRoleType())){
					searchCondition.setMerNo(logingOperators.getAccountNo());
				}else if(Role.AGENT.getCode().equals(logingOperators.getRoleType())){
					if(searchCondition.getAgentId()<=0){
						//查询出所有的下级代理ID
						List<Operators> list = merchantServices.listAllSubMerchants(logingOperators.getId());
						if(list!=null&&list.size()>0){
							long[] st =listtoarry(list);
							searchCondition.setAgentIds(st);
						}else{
							long[] st={logingOperators.getId()};
							searchCondition.setAgentIds(st);
						}
						
					}
				}else{
					searchCondition.setOrgCode(logingOperators.getOrgCode());
				}
			}
			
			
			int rows = pagenitionService.countSQL(searchCondition);
			List<SettingBean> settingList = new ArrayList<SettingBean>();
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				if (searchRsList != null && searchRsList.size() > 0) {
					Iterator iterator = searchRsList.iterator();
					while (iterator.hasNext()) {
						Object[] obj = (Object[]) iterator.next();
						SettingBean sett = new SettingBean();
						if (obj[0] != null && !"".equals(obj[0])) {
							sett.setOrderno(obj[0].toString());
						}
						if (obj[1] != null && !"".equals(obj[1])) {
							sett.setMerchantno(obj[1].toString());
						}
						if (obj[2] != null && !"".equals(obj[2])) {
							sett.setMerchantname(obj[2].toString());
						}
						if (obj[3] != null && !"".equals(obj[3])) {
							sett.setSettledate(obj[3].toString());
						}
						if (obj[4] != null && !"".equals(obj[4])) {
							sett.setFreezeflag(obj[4].toString());
						}
						if (obj[5] != null && !"".equals(obj[5])) {
							sett.setTotalamt(Double.valueOf(obj[5].toString()));
						}
						if (obj[6] != null && !"".equals(obj[6])) {
							sett
									.setTotalnum(Integer.valueOf(obj[6]
											.toString()));
						}
						if (obj[7] != null && !"".equals(obj[7])) {
							sett.setMerchantfee(Double.valueOf(obj[7]
									.toString()));
						}
						if (obj[8] != null && !"".equals(obj[8])) {
							sett.setMerchantsettleamt(Double.valueOf(obj[8]
									.toString()));
						}
						if (obj[9] != null && !"".equals(obj[9])) {
							sett.setSettleflag(obj[9].toString());
						}
						if (obj[10] != null && !"".equals(obj[10])) {
							sett.setBatchno(obj[10].toString());
						}

						settingList.add(sett);
					}
				}
			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute("roleT", logingOperators.getRoleType());
		    getRequest().setAttribute("merNo",logingOperators.getAccountNo());
		    getRequest().setAttribute("agenid",searchCondition.getAgentId());
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("settingList", settingList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);

		}catch (Exception e) {
			logger.error(e.getMessage(), e);

		}
		return SUCCESS;
	}
	
public long[] listtoarry(List<Operators> list){
	long[] st=new long[list.size()] ;
	for(int i=0;i<list.size();i++){
		st[i]=list.get(i).getId();
	}
	return st;
}

	
	// 结算初始页面
	@Action(value = "settingtotallist", results = {
			@Result(name = "success", location = "/pages/setting/settingtotallist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp") }

	)
	@SuppressWarnings("unused")
	//@Validations(requiredStrings = { @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "user.userAccount", message = "请输入[登陆用户]") })
	public String settingtotallist()throws Exception  {
		initSomeDataForAction();
		
		try {
			Operators logingOperators=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			orgList=orgService.listOrgs(logingOperators);
			
				getRequest().setAttribute("roleT", logingOperators.getRoleType());
			     getRequest().setAttribute("merNo",logingOperators.getAccountNo());
				//getRequest().setAttribute("roleT","10");
				//getRequest().setAttribute("merNo","5");
			
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			addActionError("加载机构信息出错！！");
		}
		return SUCCESS;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (SettingTotalSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_TOTALSETTING);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_TOTALSETTING, searchCondition);
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the profitRsList
	 */
	public List<Object> getProfitRsList() {
		return profitRsList;
	}

	/**
	 * @param profitRsList
	 *            the profitRsList to set
	 */
	public void setProfitRsList(List<Object> profitRsList) {
		this.profitRsList = profitRsList;
	}

	/**
	 * @return the profitModelHead
	 */

	public Operators getOperators() {
		return operators;
	}

	public void setOperators(Operators operators) {
		this.operators = operators;
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return null;
	}

	public SettingTotalSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(SettingTotalSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}
	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

}
